package com.fight.util.file;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

/**
* @ClassName: FileUtil
* @Description: 文件操作类
* @author: 温泉 
* @company: 上海航盛实业有限公司 
* @date 2012-04-22 21:54:18 +0800
*/
public class FileUtil {
	
	/**
	* @Fields log : log4j句柄
	*/
	protected static Logger log = Logger.getLogger(FileUtil.class); 
	
	/**
	* @Title: getPackagePathByWildcards
	* @Description: 通过通配符查找文件夹或者文件所属的包名
	* @param String wildcards 通配符
	* @param String separator 分隔符
	* @return String  根据分隔符得出包名
	* @author: 温泉
	* @date 2012-04-22 22:00:52 +0800
	* @throws
	*/
	public static String getPackagePathByWildcards(String wildcards,String separator){
		StringBuffer result = new StringBuffer();
		try {
			ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver(); 
			Resource[] source = resourceLoader.getResources(wildcards);
	        for (int i = 0; i < source.length; i++) { 
	            Resource resource = source[i]; 
	            String uri = resource.getURL().toString();
	            String tmp = "";
	            //代表中jar包中扫描的结果
	            if(uri.startsWith("jar:")||uri.startsWith("wsjar:")){
	            	tmp = (uri.substring((uri.indexOf(".jar!")+6),uri.length()-1)).replace("/", ".");
	            }else if(uri.startsWith("file:")){//代表从文件中扫描的结果
	            	tmp = (uri.substring((uri.indexOf("classes")+8),uri.length()-1)).replace("/", ".");
	            }
	            if(i==0){
	            	result.append(tmp);
	            }else{
	            	result.append(separator);
	            	result.append(tmp);
	            }
	        } 
		} catch (IOException e) {
			log.error("获取包路径失败，异常如下："+e.getMessage());
		} 
		return result.toString();
	}
	
	
	
	  public static boolean createDir(String destDirName) {  
	        File dir = new File(destDirName);  
	        if (dir.exists()) {  
	        	log.error("创建目录" + destDirName + "失败，目标目录已经存在");  
	            return false;  
	        }  
	        if (!destDirName.endsWith(File.separator)) {  
	            destDirName = destDirName + File.separator;  
	        }  
	        //创建目录  
	        if (dir.mkdirs()) {  
	        	log.error("创建目录" + destDirName + "成功！");  
	            return true;  
	        } else {  
	        	log.error("创建目录" + destDirName + "失败！");  
	            return false;  
	        }  
	    }  
	
	
	
	
	
	
	
	
	
}
